package LeetCode.month03.day77;

/**
 * @author 16778
 * @date 2025/10/17 9:24
 * @description
 */
public class No990 {
    public boolean equationsPossible(String[] equations) {
        int[] parent = new int[26];
        for (int i = 0; i < parent.length; i++) {
            parent[i] = i;
        }
        for (String equation : equations) {
            if(equation.charAt(1)=='='){
                int index1 = equation.charAt(0)-'a';
                int index2 = equation.charAt(3)-'a';
                union(parent,index1,index2);
            }
        }
        for (String equation : equations) {
            if(equation.charAt(1)=='!'){
                int index1 = equation.charAt(0)-'a';
                int index2 = equation.charAt(3)-'a';
                if(find(parent,index1)==find(parent,index2)) return false;
            }
        }
        return true;
    }

    public void union(int[] parent,int index1,int index2){
        parent[find(parent,index1)] = find(parent,index2);
    }

    public int find(int[] parent,int index){
        while(parent[index]!=index){
            parent[index] = parent[parent[index]];
            index = parent[index];
        }
        return index;
    }
}
